﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
using System.Data;
using System.DirectoryServices;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Get user name from cookie
        String encryptedTicket = Request.Cookies.Get(FormsAuthentication.FormsCookieName).Value;
        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(encryptedTicket);
        //Get ID from Active Directory
        DirectoryEntry entry = new DirectoryEntry("LDAP://campus.up.edu/" + SharedFunctions.GetDistinguishedName(authTicket.Name).Substring(7));
        String IDnumber = entry.Properties["employeeID"].Value.ToString();
        entry.Close();
        //Get user level
        SharedFunctions.UserLevel ul = SharedFunctions.getUserLevel(authTicket.Name);
        if (ul.Equals(SharedFunctions.UserLevel.ADVISEE)
            && ((!authTicket.Name.Equals("burich12")) && (!authTicket.Name.Equals("bergquis13")) && (!authTicket.Name.Equals("yamamoto13")) && (!authTicket.Name.Equals("ariola13"))))
        {
            Response.Redirect("~/Advisee/Default.aspx");
        }
        Session["Advisor_advisorID"] = IDnumber;

        //////////////////
        //DEBUGGING ONLY//
        //////////////////
        if ((authTicket.Name.Equals("burich12")) || (authTicket.Name.Equals("bergquis13")) || (authTicket.Name.Equals("yamamoto13")) || (authTicket.Name.Equals("ariola13")))
        {
            Session["Advisor_advisorID"] = "001050104";
            ul = SharedFunctions.UserLevel.ADMIN;
        }
        //////////////////
        //DEBUGGING ONLY//
        //////////////////

        //Get other relevant info from database
        String result = "";
        Dictionary<String, Object> results = StoredProcedures.getEmployeeName((String)Session["Advisor_advisorID"]);
        if (results != null)
        {
            Session["Advisor_firstName"] = results["firstName"];
            Session["Advisor_lastName"] = results["lastName"];
            result = result + results["lastName"] + ", " + results["firstName"] + System.Environment.NewLine;
        }
        AdvisorName.Text = "";
        Session["isAdmin"] = false;
        if (ul.Equals(SharedFunctions.UserLevel.ADMIN))
        {
            Session["isAdmin"] = true;
            AdvisorName.Text = "ADMIN: ";
        }
        AdvisorName.Text += result;
    } //end Page_Load()

    /// <summary>
    /// 
    /// Allows user to redirect to the default page by clicking on the logo
    /// in the upper right corner.
    /// 
    /// Author: Kekai Ariola
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void logo_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Advisor/Default.aspx");
    }

    /// <summary>
    /// signOut_Click()
    /// 
    /// Signs a user out of the application when the Sign Out button is clicked
    /// </summary>
    /// <param name="sender">object sender (unused, required by ASP.NET) - web control that called the function</param>
    /// <param name="e">EventArgs e (unused, required by ASP.NET) - other arguments for the function</param>
    protected void signOut_Click(object sender, EventArgs e)
    {
        //purge all advisee transcripts the advisor has viewed this session
        StoredProcedures.purgeAdvisorsAdviseesTranscripts("" + Session["Advisor_advisorID"]);
        FormsAuthentication.SignOut();
        Response.Redirect("~/Default.aspx?logout=success");
    } // end signOut_Click()
}
